/**
 * Created by zy on 2016/7/7.
 * 地图 - 详情 - 缺陷维修管理 - 管体缺陷
 */

/* 地图 - 详情 - 缺陷维修管理 - 管体缺陷 */

;(function ($, window, document, undefined) {


    // 缺陷点位置信息表格
    var $defect_table = $('#JQ_defect_table');
    // 定位位置展示表格
    var $location_table = $('#JQ_location_table');

    // 缺陷点ID
    var defectInfoId = $('#JQ_defectInfoId').val();
    // 项目名称
    var projectName = $('#JQ_projectName').val();

    // 缺陷点信息模板
    var defectTemplate = [
        {name: 'pipeName', title: '管道名称：'},
        {name: 'customNumber', title: '缺陷点编号：'},
        {name: 'mileage', title: '绝对距离(m)：'},
        {name: 'featureSubType', title: '缺陷类型：'},
        {
            name: 'length', title: '长/宽/深(mm)：', render: function (r) {
            return r.length + '/' + r.width + '/' + r.depth;
        }
        },
        {name: 'clock', title: '时钟位置(h:min)：'},
        {name: 'erf', title: 'ERF：'},
        {name: 'repairSuggestion', title: '修复方法：'},
        {name: 'repairState_label', title: '修复状态：'}
    ];

    // 地位信息模板
    var locationTemplate = [
        {name: 'typeName', title: '类型'},
        {name: 'mileage', title: '绝对距离(m)'},
        {name: 'relativeMileage', title: '距目标点距离(m)'},
        {name: 'fromCrossClock', title: '上游交汇时钟位置'},
        {name: 'toCrossClock', title: '下游交汇时钟位置'}
    ];

    // gis地图参数
    var gisJsonArr = [
        {
            'typeName': 'pipeLineId',       // 管道中心线
            'gisIdArray': []
        },
        {
            'typeName': 'inspDefect',       // 缺陷点
            'gisIdArray': []
        },
        {
            'typeName': 'trackPoint',       // mark点
            'gisIdArray': []
        },
        {
            'typeName': 'inspElbow',        // 弯头
            'gisIdArray': []
        },
        {
            'typeName': 'inspGww',          // 弯头
            'gisIdArray': []
        },
        {
            'typeName': 'valveRoomPoint',
            'gisIdArray': []
        }
    ];

    if (defectInfoId) {
        DyyBase.ajax(defectApi.url_repair_map_query(), {defectInfoId: defectInfoId}, function (dataset) {
            if (dataset.bool) {
                var defect = DyyBase.clearNull(dataset.result.defectInfo);
                fillDefect(defect);
                var location = DyyBase.clearNull(dataset.result.nearbyLocation);
                fillLocation(location);
                // 注入管道gis
                gisJsonArr[0]['gisIdArray'].push(dataset.result.pipeLineCenterLine.gisId);
                // 注入场站阀室gis
                gisJsonArr[5]['gisIdArray'] = dataset.result.valveRoomGisIds;
            }
        });
    }

    // 填充缺陷信息
    function fillDefect(data) {
        if (data) {
            var dd = '';
            for (var i = 0, len = defectTemplate.length; i < len; i++) {
                if (data.hasOwnProperty(defectTemplate[i]['name'])) {
                    var key = defectTemplate[i]['title'];
                    var value = data[defectTemplate[i]['name']];
                    if (defectTemplate[i].hasOwnProperty('render')) {
                        if (defectTemplate[i]['render'] instanceof Function) {
                            value = defectTemplate[i]['render'](data);
                        }
                    }
                    dd += '<dd><span>' + key + '</span><span title="' + value + '">' + value + '</span></dd>';
                }
            }
            if (dd) {
                $defect_table.empty().html(dd);
            }
            // 注入gisid
            gisJsonArr[1]['gisIdArray'].push(data['gisId']);
        }
    }

    // 填充定位信息
    function fillLocation(data) {
        if (data) {
            $location_table.empty();
            for (var i = 0, len = data.length; i < len; i++) {
                var idx = i;
                var $tr = $('<tr></tr>'), td = '<td>' + (idx) + '</td>';
                for (var j = 0, lens = locationTemplate.length; j < lens; j++) {
                    if (data[i].hasOwnProperty(locationTemplate[j]['name'])) {
                        var value = data[i][locationTemplate[j]['name']];
                        if (locationTemplate[j]['name'] == 'mileage' || locationTemplate[j]['name'] == 'relativeMileage') {
                            var temp = value.toString();
                            temp = temp.split('.');
                            if (temp.length === 2) {
                                if (temp[1].length > 3) {
                                    temp[1] = temp[1].substr(0, 3);
                                    if (temp[1].substr(2, 1) === '0') {
                                        temp[1] = temp[1].substr(0, 2);
                                        if (temp[1].substr(1, 1) === '0') {
                                            temp[1] = temp[1].substr(0, 1);
                                            if (temp[1].substr(0, 1) === '0') {
                                                temp[1] = '';
                                            }
                                        }
                                    }
                                }
                                if (temp[1]) {
                                    temp = temp[0] + '.' + temp[1];
                                } else {
                                    temp = '0';
                                }
                            }
                            value = temp;
                        }
                        td += '<td>' + value + '</td>';
                    }
                }
                $tr.html(td);
                $location_table.append($tr);

                // 注入gisid
                switch (data[i]['typeName']) {
                    case 'MARK点':
                        if (data[i]['gisId'] && data[i]['gisId'] !== '-') {
                            gisJsonArr[2]['gisIdArray'].push(data[i]['gisId']);
                        }
                        break;
                    case '弯头':
                        if (data[i]['gisId'] && data[i]['gisId'] !== '-') {
                            gisJsonArr[3]['gisIdArray'].push(data[i]['gisId']);
                        }
                        break;
                    case '环焊缝':
                        if (data[i]['gisId'] && data[i]['gisId'] !== '-') {
                            gisJsonArr[4]['gisIdArray'].push(data[i]['gisId']);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }

    // 加载地图api
    var dyMapIfarme, dyMap, curent_map;
    dyMapIfarme = $('#JQ_dyMap').get(0);
    // 地图加载完毕
    dyMapIfarme.onload = function () {
        dyMap = dyMapIfarme.contentWindow.queryMutiLayers;
        curent_map = dyMapIfarme.contentWindow.basePipeMap.currentMap;
        // 定位地图
        //console.log(gisJsonArr);
        dyMap(curent_map,gisJsonArr);
    };


    // 返回
    $('#JQ_go_back').click(function () {
        window.history.go(-1);
    });

})(jQuery, window, document);